package com.jsbs.iam.ident.controller;
import com.jsbs.iam.common.core.dto.Result;
import com.jsbs.iam.common.core.exception.ResultException;
import com.jsbs.iam.common.core.utils.ExceptionUtil;
import com.jsbs.iam.common.core.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class BaseController {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseController.class);

    /**
     * 处理异常
     *
     * @param e
     * @param errMsg 指定返回给前端的信息
     * @return
     */
    protected Result handleException(Throwable e, String errMsg) {
        if (e instanceof ResultException) {
            return ((ResultException) e).getResult();
        }
        LOGGER.warn(ExceptionUtil.getDetail(e));
        if (StringUtils.isNotEmpty(errMsg)) {
            return Result.failWithMsg(errMsg);
        }
        return Result.serverError();
    }

    /**
     * 处理异常
     *
     * @param e
     * @return
     */
    protected Result handleException(Throwable e) {
        if (e instanceof ResultException) {
            return ((ResultException) e).getResult();
        }
        LOGGER.error(ExceptionUtil.getDetail(e));
        return Result.serverError();
    }
}
